<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加教室信息' : '编辑教室信息'" :visible.sync="addEditIsDialog_c" :close-on-click-modal="false" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="24">
            <el-form-item label="考场名称：" prop="examinationRoomId">
              <el-select v-model="ruleForm.examinationRoomId" placeholder="请选择考场名称" clearable @change="schoolChange" :disabled="seeType || userType != 0">
                <el-option v-for="item in schoolArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="学院名称：" prop="collegeId">
              <el-select v-model="ruleForm.collegeId" placeholder="请选择学院名称" @change="collegeChange" clearable>
                <el-option v-for="item in collegeArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="院系名称：" prop="facultyId">
              <el-select v-model="ruleForm.facultyId" placeholder="请选择院系名称" @change="facultyChange" clearable>
                <el-option v-for="item in facultyArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="楼层名称：" prop="floorId">
              <el-select v-model="ruleForm.floorId" placeholder="请选择楼层名称" clearable>
                <el-option v-for="item in floorArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="教室编码：" prop="code">
              <el-input v-model="ruleForm.code" placeholder="请输入教室编码" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="教室名称：" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请输入教室名称" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="教室地址：" prop="address">
              <el-input v-model="ruleForm.address" placeholder="请输入教室地址" clearable maxlength="100" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="排序号：" prop="orderNum">
              <el-input-number v-model="ruleForm.orderNum" placeholder="请输入排序号" :precision="0" :step="1" :max="9999" :min="0" :disabled="seeType"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getClassroomInfo, saveClassroomInfo, getSchoolInfos } from '@/api/school'

export default {
  props: {
    addEditId: {
      type: String,
      default: ''
    },
    addEditStatus: {
      type: String,
      default: 'add'
    },
    addEditIsDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: '',
        examinationRoomId: '',
        collegeId: '',
        facultyId: '',
        floorId: '',
        code: '',
        name: '',
        address: '',
        orderNum: ''
      },
      rules: {
        // 表单规则
        examinationRoomId: [{ required: true, message: '请选择考场名称', trigger: 'change' }],
        collegeId: [{ required: true, message: '请选择学院名称', trigger: 'change' }],
        facultyId: [{ required: true, message: '请选择院系名称', trigger: 'change' }],
        floorId: [{ required: true, message: '请选择楼层名称', trigger: 'change' }],
        code: [{ required: true, message: '请输入教室编码', trigger: 'blur' }],
        name: [{ required: true, message: '请输入教室名称', trigger: 'blur' }],
        address: [{ required: true, message: '请输入教室地址', trigger: 'blur' }],
        orderNum: [{ required: true, message: '请输入排序号', trigger: 'blur' }]
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      schoolArray: [], // 考场集合
      collegeArray: [], // 学院集合
      facultyArray: [], // 院系集合
      floorArray: [], // 楼层集合
      userType: '', // 用户类型（0超级管理员，1考场管理员）
      examinationRoomId: '' // 考场id
    }
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val
      if (this.addEditIsDialog_c) {
        this.userType = Number(window.localStorage.getItem('userType'))
        this.examinationRoomId = Number(window.localStorage.getItem('examinationRoomId'))
        if (this.userType == 1) {
          this.ruleForm.examinationRoomId = this.examinationRoomId

          if (this.addEditStatus == 'add') {
            // 获取学院列表
            this.getCollegeList()

            // 获取院系列表
            this.getFacultyList()

            // 获取楼层列表
            this.getFloorList()
          }
        }

        // 添加
        if (this.addEditStatus == 'add') {
          this.ruleForm.id = ''
          this.seeType = false
        }

        // 编辑
        if (this.addEditStatus == 'edit') {
          this.ruleForm.id = this.addEditId
          this.seeType = false
          this.getDetail()
        }

        // 查看
        if (this.addEditStatus == 'see') {
          this.ruleForm.id = this.addEditId
          this.seeType = true
          this.getDetail()
        }

        // 获取考场列表
        this.getSchoolList()
      }
    }
  },
  methods: {
    // 详情
    getDetail() {
      getClassroomInfo({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data

          // 获取学院列表
          this.getCollegeList()

          // 获取院系列表
          this.getFacultyList()

          // 获取楼层列表
          this.getFloorList()
        }
      })
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveClassroomInfo(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })

              // 关闭弹窗
              this.$parent.closeAddEdit()
              // 刷新列表
              this.$parent.onSubmit()
            }
          })
        }
      })
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.$parent.closeAddEdit()
    },
    // 获取考场列表
    getSchoolList() {
      getSchoolInfos().then((res) => {
        if (res.code == 200) {
          this.schoolArray = res.data
        }
      })
    },
    // 考场下拉切换
    schoolChange() {
      this.ruleForm.collegeId = ''
      this.collegeArray = []

      this.ruleForm.facultyId = ''
      this.facultyArray = []

      this.ruleForm.floorId = ''
      this.floorArray = []

      // 获取学院列表
      this.getCollegeList()
    },

    // 学院下拉切换
    collegeChange() {
      this.ruleForm.facultyId = ''
      this.facultyArray = []

      this.ruleForm.floorId = ''
      this.floorArray = []

      // 获取院系列表
      this.getFacultyList()
    },
    // 获取院系列表
    getFacultyList() {
      if (!this.ruleForm.collegeId || this.ruleForm.collegeId == '') {
        return
      }

      getFacultyInfos({ id: this.ruleForm.collegeId }).then((res) => {
        if (res.code == 200) {
          this.facultyArray = res.data
        }
      })
    },
    // 院系下拉切换
    facultyChange() {
      this.ruleForm.floorId = ''
      this.floorArray = []

      // 获取楼层列表
      this.getFloorList()
    },
    // 获取楼层列表
    getFloorList() {
      if (!this.ruleForm.facultyId || this.ruleForm.facultyId == '') {
        return
      }

      getFloorInfos({ id: this.ruleForm.facultyId }).then((res) => {
        if (res.code == 200) {
          this.floorArray = res.data
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }
}
</style>
